/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.pingan.haofang.searchcloud.api.index.IndexNameGeter;

/**
 * 
 * DefaultIndexPipePacket is :
 * 
 * @author huhailiang@baidu.com
 * @date 2015年12月30日
 *
 */
public class IndexDataPacket implements DataHold<IndexRowData>,IndexNameGeter {

    /**
     * 索引名称
     */
    private String indexName;

    /**
     * 数据列表
     */
    private List<IndexRowData> indexRowData;

    public IndexDataPacket(String indexName) {
        this.indexName = indexName;
        initIndexRowData();
    }

    public IndexDataPacket build(IndexRowData rowData) {
        if (null == rowData) {
            return this;
        }
        initIndexRowData();
        indexRowData.add(rowData);
        return this;
    }

    public IndexDataPacket build(List<IndexRowData> rowDatas) {
        if (null == rowDatas || rowDatas.isEmpty()) {
            return this;
        }
        initIndexRowData();
        indexRowData.addAll(rowDatas);
        return this;
    }

    private void initIndexRowData() {
        indexRowData = (null == indexRowData) ? new ArrayList<IndexRowData>() : indexRowData;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public List<IndexRowData> getIndexRowData() {
        return indexRowData;
    }

    public void setIndexRowData(List<IndexRowData> indexRowData) {
        this.indexRowData = indexRowData;
    }

    @Override
    public List<IndexRowData> getUpdateData() {
        if (null == indexRowData) {
            return Collections.emptyList();
        }
        List<IndexRowData> indexUpdateRowData = new ArrayList<IndexRowData>();
        for (IndexRowData row : indexRowData) {
            if (!OperateEnum.DELETE.equals(row.getOperate())) {
                indexUpdateRowData.add(row);
            }
        }
        return indexUpdateRowData;
    }

    @Override
    public List<IndexRowData> getDeleteData() {
        if (null == indexRowData) {
            return Collections.emptyList();
        }
        List<IndexRowData> indexDeleteRowData = new ArrayList<IndexRowData>();
        for (IndexRowData row : indexRowData) {
            if (OperateEnum.DELETE.equals(row.getOperate())) {
                indexDeleteRowData.add(row);
            }
        }
        return indexDeleteRowData;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }

    
}
